﻿using System;
using Combat;
using TMPro;
using UnityEngine;

namespace UI
{
    public class HealthUi : MonoBehaviour
    {
        [SerializeField] private Health health;
        
        [SerializeField] private Color normalColor;
        [SerializeField] private Color damagedColor;
        [SerializeField] private Color dangerousColor;

        private TextMeshProUGUI _text;


        private void Awake()
        {
            _text = GetComponent<TextMeshProUGUI>();
            
            health.HealthChanged += Health_HealthChanged;
        }

        private void Health_HealthChanged()
        {
            _text.text = $"{health.NormalizedHp * 100f:0}%";

            if (health.NormalizedHp <= 0.1f)
            {
                _text.color = dangerousColor;
            }
            else if (health.NormalizedHp <= 0.5f)
            {
                _text.color = damagedColor;
            }
            else
            {
                _text.color = normalColor;
            }
        }
    }
}